package com.usian.article.controller;

import com.usian.apis.article.ApArticleControllerApi;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApArticleController implements ApArticleControllerApi {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleService apArticleService;
    private int i;

    @Override
    @PostMapping("/api/v1/article/save")
    public ApArticle saveArticle(@RequestBody ApArticle apArticle) {
        if (apArticle == null) return null;
        apArticleMapper.insert(apArticle);
        return apArticle;
    }

    @Override
    @PostMapping("/api/v1/article_config/save")
    public ResponseResult saveArticleConfig(@RequestBody ApArticleConfig apArticleConfig) {
        if (apArticleConfig == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        apArticleConfigMapper.insert(apArticleConfig);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @PostMapping("/api/v1/article_content/save")
    public ResponseResult saveArticleContent(@RequestBody ApArticleContent apArticleContent) {
        if (apArticleContent == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        apArticleContentMapper.insert(apArticleContent);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @PostMapping("/api/v1/article/load_article_info")
    public ResponseResult loadArticleInfo(@RequestBody ArticleInfoDto dto) {
        i = 1 / 2;
        return apArticleService.loadArticleInfo(dto);
    }
}
